package com.falcon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.config.FileConfig;
import com.falcon.core.utils.BeanUtils;
import com.falcon.domain.dos.FileDO;
import com.falcon.domain.req.FileQuery;
import com.falcon.domain.req.FileRequest;
import com.falcon.mapper.IFileMapper;
import com.falcon.mvc.anno.LogTime;
import com.falcon.service.IFileService;
import com.falcon.storage.IFileStorage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.falcon.domain.po.FilePO;
import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.PagerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author zongf
 * @since 2023-04-20
 **/
@Slf4j
@Service
public class FileService implements IFileService {

    @Autowired
    private IFileMapper fileMapper;

    @Autowired
    private IFileStorage fileUploader;

    @Autowired
    private FileConfig fileConfig;

    @Override
    public Long save(FileRequest request) {
        FilePO filePO = BeanUtil.copyProperties(request, FilePO.class);

        this.fileMapper.insertNotNull(filePO);

        return filePO.getId();
    }

    @Override
    public Boolean update(Long id, FileRequest request) {

        FilePO filePO = BeanUtil.copyProperties(request, FilePO.class);
        filePO.setId(id);

        return this.fileMapper.updateNotNull(filePO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.fileMapper.deleteById(id);
    }

    @LogTime
    @Override
    public FileDO queryInfoById(Long id) {

        FilePO filePO = this.fileMapper.selectById(id);

        FileDO fileDO = BeanUtil.copyProperties(filePO, FileDO.class);

        return fileDO;
    }


    @Override
    public List<FileDO> queryList(FileQuery request) {

        Condition condition = request.toCondition();

        List<FilePO> poList = this.fileMapper.selectList(condition);

        List<FileDO> doList = BeanUtil.copyToList(poList, FileDO.class);

        return doList;
    }

    @Override
    public PagerResponse<FileDO> queryPager(FileQuery request) {


        PageInfo<FilePO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.fileMapper.selectList(request.toCondition()));

        return BeanUtils.copyToPager(pageInfo, FileDO.class);
    }


}

